package com.atguigu3;

import com.beans.Customers;
import com.utils.JDBCUtils;
import org.junit.Test;

import java.lang.reflect.Field;
import java.sql.*;
import java.util.Arrays;

/**
 * 针对Customers表的查询操作
 * @auther user
 * @date 2021/12/7 - 0:53
 */
public class CustomersForQueryTest {
    /**
     * 三种不同的数据封装方式
     */
    @Test
    public void QueryForCustomers() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            //1 获取连接
            connection = JDBCUtils.getConnection();
            //2 预编译sql,返回PreparedStatment实例
            String sql = "select id,name,email,birth from customers where id = ?";
            preparedStatement = connection.prepareStatement(sql);
            //3 填充占位符
            preparedStatement.setObject(1,1);
            //4 执行，返回resultSet
            resultSet = preparedStatement.executeQuery();
            //5 处理结果集
            if (resultSet.next()){
                //获取各个字段值
                int id = resultSet.getInt(1);
                String name = resultSet.getString(2);
                String email = resultSet.getString(3);
                Date birth = resultSet.getDate(4);
                //方式一：打印各个字段
                System.out.println("id = " + id);
                System.out.println("name = " + name);
                System.out.println("email = " + email);
                System.out.println("birth = " + birth);
                //方式二：封装进数组
                Object[] objects = new Object[] {id,name,email,birth};
                System.out.println(Arrays.toString(objects));
                //方式三：封装进对象（推荐）
                Customers customers = new Customers(id, name, email, birth);
                System.out.println(customers);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {

            //6 关闭资源
            JDBCUtils.closeResource(connection,preparedStatement,resultSet);
        }
    }

    /**
     *  针对Customers表的通用查询操作
     * @param sql
     * @param args
     * @return
     */
    public Customers queryForCustomers(String sql,Object ...args) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            //1 获取连接
            connection = JDBCUtils.getConnection();
            //2 预编译sql,返回preparedStatement实例
            preparedStatement = connection.prepareStatement(sql);
            //3 填充占位符
            for (int i = 0; i < args.length; i++) {
                preparedStatement.setObject(i+1,args[i]);
            }
            //4 执行并返回ResultSet结果集
            resultSet = preparedStatement.executeQuery();
            //5 处理结果集
            //获取元数据
            ResultSetMetaData metaData = resultSet.getMetaData();
            //获取总列数
            int columnCount = metaData.getColumnCount();
            if (resultSet.next()){
                Customers customers = new Customers();
                for (int i = 0; i < columnCount; i++) {
                    //获取每列的别名
                    String columnLabel = metaData.getColumnLabel(i + 1);
                    //获取列值
                    Object columnValue = resultSet.getObject(i + 1);
                    //给customers对象属性赋值
                    Field field = Customers.class.getDeclaredField(columnLabel);
                    field.setAccessible(true);
                    field.set(customers,columnValue);
                }
                return customers;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //关闭资源
            JDBCUtils.closeResource(connection,preparedStatement,resultSet);
        }
        return null;
    }
    @Test
    public void testQueryForCustomers(){
        String sql = "select id,name,email,birth from customers where id = ?";
        Customers customers = queryForCustomers(sql, 3);
        System.out.println(customers);

        sql = "select id,name from customers where id = ?";
        customers = queryForCustomers(sql, 3);
        System.out.println(customers);
    }
}
